package com.abbas.abchat.adapter;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.abbas.abchat.AbChatApplication;
import com.abbas.abchat.R;
import com.abbas.abchat.activity.PeopleDetailActivity;
import com.abbas.abchat.entity.Account;
import com.abbas.abchat.entity.ChatMsg;
import com.abbas.abchat.utils.TimeUtils;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;

import java.util.List;

/**
 * @author abbas
 * @description: 多布局
 * @date 2022/4/14 23:09
 */
public class ChatListAdapter extends BaseAdapter {

    private Context mContext;
    private List<ChatMsg> msg;

    private Account LAccount; //LoginAccount 登录账号
    private Account PAccount; //PeopleAccount 对方账号

    private String chatDbName;

    public static final int TYPE1 = 0; //对方消息
    public static final int TYPE2 = 1; //我方消息
    public static final int TYPE3 = 2; //日期提示

    public ChatListAdapter(Context mContext, List<ChatMsg> msg,Account LAccount,Account PAccount,String chatDbName) {
        this.mContext = mContext;
        this.msg = msg;
        this.LAccount = LAccount;
        this.PAccount = PAccount;
        this.chatDbName = chatDbName;
    }

    public void setMsg(List<ChatMsg> msg) {
        this.msg = msg;
        this.notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return msg.size();
    }

    @Override
    public Object getItem(int position) {
        return msg.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getViewTypeCount() {
        return 3;
    }

    @Override
    public int getItemViewType(int position) {
        ChatMsg chatMsg = msg.get(position);
        if (chatMsg.getType() == 1) return TYPE3;
        int id = chatMsg.getpId();
        if (id == LAccount.getId()){
            return TYPE2;
        }else if (id == PAccount.getId()){
            return TYPE1;
        }else{
            return TYPE3;
        }
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LeftViewHolder leftViewHolder = null;
        RightViewHolder rightViewHolder = null;
        TipViewHolder tipViewHolder = null;

        ChatMsg chatMsg = msg.get(position);
        int type = getItemViewType(position);

        if (convertView == null){
            switch (type){
                case TYPE1:
                    leftViewHolder = new LeftViewHolder();
                    convertView = LayoutInflater.from(mContext).inflate(R.layout.chat_left_item,null,false);
                    leftViewHolder.img = (ImageView) convertView.findViewById(R.id.img);
                    leftViewHolder.content = (TextView) convertView.findViewById(R.id.content);
                    convertView.setTag(leftViewHolder);
                    break;
                case TYPE2:
                    rightViewHolder = new RightViewHolder();
                    convertView = LayoutInflater.from(mContext).inflate(R.layout.chat_right_item,null,false);
                    rightViewHolder.img = (ImageView) convertView.findViewById(R.id.img);
                    rightViewHolder.content = (TextView) convertView.findViewById(R.id.content);
                    convertView.setTag(rightViewHolder);
                    break;
                case TYPE3:
                    tipViewHolder = new TipViewHolder();
                    convertView = LayoutInflater.from(mContext).inflate(R.layout.chat_tip_item,null,false);
                    tipViewHolder.chatTag = (TextView) convertView.findViewById(R.id.chat_tag);
                    convertView.setTag(tipViewHolder);
                    break;
                default:
                    break;
            }
        }else {
            switch (type){
                case TYPE1:
                    leftViewHolder = (LeftViewHolder) convertView.getTag();
                    break;
                case TYPE2:
                    rightViewHolder = (RightViewHolder) convertView.getTag();
                    break;
                case TYPE3:
                    tipViewHolder = (TipViewHolder) convertView.getTag();
                    break;
                default:
                    break;
            }
        }

        switch (type){
            case TYPE1:
                if (!TextUtils.isEmpty(PAccount.getImgPath())){
                    Glide.with(mContext)
                            .load(PAccount.getImgPath())
                            .transform(new RoundedCorners(12))
                            .into(leftViewHolder.img);
                }else {
                    Glide.with(mContext)
                            .load(R.drawable.ic_people_24)
                            .transform(new RoundedCorners(12))
                            .into(leftViewHolder.img);
                }
                leftViewHolder.content.setText(String.valueOf(chatMsg.getContent()));

                leftViewHolder.img.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(mContext, PeopleDetailActivity.class);
                        intent.putExtra("account",PAccount);
                        intent.putExtra("dbName",chatDbName);
                        mContext.startActivity(intent);
                    }
                });
                break;
            case TYPE2:
                if (!TextUtils.isEmpty(LAccount.getImgPath())){
                    Glide.with(mContext)
                            .load(LAccount.getImgPath())
                            .transform(new RoundedCorners(12))
                            .into(rightViewHolder.img);
                }else {
                    Glide.with(mContext)
                            .load(R.drawable.ic_people_24)
                            .transform(new RoundedCorners(12))
                            .into(rightViewHolder.img);
                }
                rightViewHolder.content.setText(String.valueOf(chatMsg.getContent()));
                break;
            case TYPE3:
                tipViewHolder.chatTag.setText(TimeUtils.getInstance(chatMsg.getDate()).compareOfChatTag());
                break;
        }

        return convertView;
    }





    static class LeftViewHolder{
        private ImageView img;
        private TextView content;
    }

    static class RightViewHolder{
        private ImageView img;
        private TextView content;
    }

    static class TipViewHolder{
        private TextView chatTag;
    }

}
